package org.wmq.travelagency02.controller;

import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;
import org.wmq.travelagency02.model.Entity.ChatGroupMember;
import org.wmq.travelagency02.model.Entity.Result;
import org.wmq.travelagency02.service.ChatGroupMemberService;

import java.time.LocalDateTime;

@RestController
@RequestMapping("/chat-group-member")
@RequiredArgsConstructor
public class ChatGroupMemberController {
    private final ChatGroupMemberService chatGroupMemberService;

    @DeleteMapping("/{chatGroupId}/{chatUserId}")
    public Result<Void> deleteByChatGroupIdAndChatUserId(@PathVariable String chatGroupId, @PathVariable String chatUserId) {
        boolean result = chatGroupMemberService.removeByChatGroupIdAndChatUserId(chatGroupId, chatUserId);
        if (result) {
            return Result.ok();
        } else {
            return Result.error("删除失败");
        }
    }

    @PostMapping
    public Result<Void> save(@RequestBody ChatGroupMember chatGroupMember) {
        chatGroupMember.setCreateTime(LocalDateTime.now());
        chatGroupMember.setUpdateTime(LocalDateTime.now());
        boolean result = chatGroupMemberService.save(chatGroupMember);
        if (result) {
            return Result.ok();
        } else {
            return Result.error("添加失败");
        }
    }
}
